package com.jc.service.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.Base64Util;
import com.jc.common.util.MD5;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.framework.exception.ServiceException;
import com.jc.service.api.order.add;
import com.jc.service.model.h5.Config;

/**
 * 
* @version:
* @Description:  接口数据预处理
* @author: zyf
* 2018年12月3日
*/

public abstract class ApiAdapter {
	private final Logger LOGGER = LoggerFactory.getLogger(ApiAdapter.class);
	
	public static Map<String,Config> config = null;
	
	public void init(String cfg) {
		Config cg ;
		if(config == null) {
			config = new HashMap<String,Config>();
			if(StringUtils.isBlank(cfg)) {
				throw new BaseServiceException("系统错误,未能正确读取接口授权配置");
			}
			try {
				
				JSONArray config_array = JSON.parseArray(cfg);
				for(int i = 0 ; config_array != null && i < config_array.size() ; i++ ) {
					JSONObject oConfig = config_array.getJSONObject(i);
					if(oConfig != null) {
						String     ooConfigKey = oConfig.getString("key");
						JSONObject ooConfig    = oConfig.getJSONObject("cfg");
						if(ooConfig != null) {
							cg = new Config();
							if(StringUtils.isNotBlank(ooConfig.getString("appkey")))cg.setAppkey(ooConfig.getString("appkey"));
							if(StringUtils.isNotBlank(ooConfig.getString("appsecret")))cg.setAppsecret(ooConfig.getString("appsecret"));
							config.put(ooConfigKey,  cg);
						}
					}
				}
			}catch (Exception e) {
				LOGGER.error("初始化接口配置错误, {},{}",e.getMessage(),e);
				throw new BaseServiceException("系统错误,未能正确读取接口授权配置,配置节点格式错误,未能正确转换为JSON");
			}
		}
	}
	/***
	 * 
	* @Title: checkSign
	* @Description: 对其它系统的请求进行验签 : MD5(BASE64(appkey+appsecret+timestamp))
	* @param @return
	* @return boolean
	* @throws
	 */
	public  boolean checkSign(ParamModel model) {
		
		Long currentTimestamp = new Date().getTime();
		if(model.getTimestamp() == null) {
			throw new ServiceException("错误, 请求缺少公共参数timestamp");
		}
		if(currentTimestamp - model.getTimestamp() > 60000) {
			throw new ServiceException("接口请求超时,属于非法请求, 请重新请求接口");
		}
		//读取接口授权配置 local ,config, redis , table,系统暂时未使用分布式,把接口授权配置先配置在配置文件中
		if(StringUtils.isBlank(model.getReqSource())) {
			throw new ServiceException("错误, 请求缺少公共参数reqSource");
		}
		Config localConfig = ApiAdapter.config.get(model.getReqSource());
		if(localConfig == null) {
			throw new ServiceException("错误, " + model.getReqSource() + "请求源未授权,请联系管理员,进行授权") ;
		}
		if(StringUtils.isBlank(localConfig.getAppkey())) throw new ServiceException("错误, 请求源授权错误,缺少appkey请联系管理员,进行授权");
		if(StringUtils.isBlank(localConfig.getAppsecret())) throw new ServiceException("错误, 请求源授权错误,缺少appsecret请联系管理员,进行授权");
		
		String local_sign =  MD5.passmd5(Base64Util.encode(localConfig.getAppkey() + localConfig.getAppsecret() + model.getTimestamp()));
		
		if(StringUtils.equals(model.getSign(), local_sign)) {
			return true;
		}else {
			throw new ServiceException("错误, 签名错误");
		}
	}
	
	
	
	
	
	
	
	

}
